package com.yufei.infoExtractor.pfw;

import java.sql.Connection;

import javax.sql.DataSource;

import org.apache.log4j.Logger;

import com.yufei.infoExtractor.util.DButils;

public class PfwBaseDao implements PfwOperation{
	private static Logger log = Logger.getLogger(PfwBaseDao.class);
    private DataSource dataSource = null;
    

	public DataSource getDataSource() {
		return dataSource;
	}


	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}


	public Object execute(PfwClientCallback action,final StringBuilder query,final StringBuilder values) {
		// TODO Auto-generated method stub
		Connection conn = null;
		long begin = System.currentTimeMillis();
		try {
			conn = dataSource.getConnection();
			//conn.setAutoCommit(false);
			
			action.doInPfwClient(conn);
			//conn.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			/*try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}*/
		}finally{
			DButils.closeConnection(conn);
			
		}
		long end = System.currentTimeMillis();

		StringBuilder sb=new StringBuilder();
		sb.append("此次数据库操作花费").append((end - begin)).append("ms]");
		log.info(sb.toString());
		return null;
	}


	public Object execute(PfwClientCallback action) {
		// TODO Auto-generated method stub
		Connection conn = null;
		Object obj=null;
		long begin = System.currentTimeMillis();
		try {
			conn = dataSource.getConnection();
			conn.setAutoCommit(false);
			
			obj=action.doInPfwClient(conn);
			conn.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			/*try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}*/
			e.printStackTrace();
			
		}finally{
			DButils.closeConnection(conn);
			
		}
		long end = System.currentTimeMillis();

		StringBuilder sb=new StringBuilder();
		sb.append("此次数据库操作花费").append((end - begin)).append("ms]");
		log.info(sb.toString());
		return obj;
	}
}
    




	
